const Controller = require("egg").Controller;


class JwtController extends Controller {
    async doLogin() {
        let user = this.ctx.request.body.user; // user.username;user.password
        if (user.username === "admin" && user.password === "123456") {
            let user_jwt = { username: user.username }
            let token = this.app.jwt.sign(user, this.app.config.jwt.secret)
            this.ctx.body = {
                code: 200,
                token: token
            }
        } else {
            this.ctx.body = {
                code: 400,
                msg: "用户名或密码错误"
            }
        }
    }

    async getMessage() {
        this.ctx.body = "hello jwt"
    }


    // async index() {
    //     let user = {
    //         username: "xiaoming"
    //     }
    //     // egg-jwt
    //     // 用户登录 生成token
    //     let token = this.app.jwt.sign(user, this.app.config.jwt.secret)
    //     // this.ctx.body = token;
    //     try {
    //         // 检验
    //         let decode = this.app.jwt.verify(token, this.app.config.jwt.secret)
    //         this.ctx.body = decode

    //     } catch (e) {
    //         this.ctx.body = "token未能通过验证"
    //     }

    // }
}

module.exports = JwtController